package com.kqb.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.ErrorResult;
import com.kqb.common.domain.page.PageResult;
import com.kqb.common.utils.DateUtils;
import com.kqb.common.utils.StringUtils;
import com.kqb.mapper.CustomerInquiryDetailMapper;
import com.kqb.mapper.CustomerInquiryMapper;
import com.kqb.pojo.Customer;
import com.kqb.pojo.CustomerInquiry;
import com.kqb.pojo.CustomerInquiryDetail;
import com.kqb.pojo.dto.CustomerInquiryDto;
import com.kqb.pojo.dto.InquiryQueryDto;
import com.kqb.pojo.vo.CustomerInquiryVo;
import com.kqb.service.ICustomerInquiryService;
import com.kqb.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 杉果树皮
 * @date 2025/2/25 16:59
 * @description:
 */
@Service
public class CustomerInquiryServiceImpl implements ICustomerInquiryService {
    @Autowired
    private CustomerInquiryMapper customerInquiryMapper;

    @Autowired
    private CustomerInquiryDetailMapper customerInquiryDetailMapper;
    private String maxCode;//最大编码

    @Override
    @Transactional
    public AjaxResult save(CustomerInquiry customerInquiry) {
        customerInquiry.setCode(generateNewCode());
        //放入id
        Map map = ThreadLocalUtils.getThreadLocalUtils().getCurrentUser();
        customerInquiry.setCid( (int)map.get("id"));
        int save = customerInquiryMapper.save(customerInquiry);
        ErrorResult.business(save, "客户询价单", "保存客户询价单失败");

        ArrayList<CustomerInquiryDetail> customerInquiryDetails = customerInquiry.getCustomerInquiryDetails();
        int i = customerInquiryDetailMapper.saveAll(customerInquiry.getId(), customerInquiryDetails);
        ErrorResult.business(i, "客户询价单详情", "保存客户询价单详情失败");

        return AjaxResult.success("添加客户询价单成功");
    }

    /**
     * 查询询价单
     *
     * @param inquiryQueryDto
     * @return
     */
    @Override
    public AjaxResult select(InquiryQueryDto inquiryQueryDto) {
        Map currentUser = ThreadLocalUtils.getThreadLocalUtils().getCurrentUser();
        int id = (int) currentUser.get("id");
        String role = ((String[]) currentUser.get("roles"))[0];
        //判断角色
        if ("admin".equals(role)) {
        } else if ("sale".equals(role)) {
            inquiryQueryDto.setSaleId(id);
        } else if ("customer".equals(role)) {
            inquiryQueryDto.setId(id);
        }
        PageHelper.startPage(inquiryQueryDto.getPageNum(), inquiryQueryDto.getPageSize());
        List<CustomerInquiryVo> list = customerInquiryMapper.select(inquiryQueryDto);
        PageInfo<CustomerInquiryVo> userPageInfo = new PageInfo<>(list);
        return AjaxResult.success("查询成功", new PageResult(inquiryQueryDto.getPageNum(), inquiryQueryDto.getPageSize(), userPageInfo.getTotal(), userPageInfo.getList()));
    }

    /**
     * 更新询价单状态
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult updateStatus(int id) {
        int i = customerInquiryMapper.updateStatus(id);
        if (i != 0) {
            return AjaxResult.success("更新成功");
        } else {
            return AjaxResult.error("更新失败");
        }
    }


    @Override
    public AjaxResult selectByInquiryId(int id) {
        return AjaxResult.success("查询询价单详情成功", customerInquiryDetailMapper.selectByInquiryId(id));
    }

    /**
     * 获取最新的询价单编码
     *
     * @return
     */
    public String generateNewCode() {
        String ym = DateUtils.getSysDateYM();
        //查询maxCode是否为空

        if (StringUtils.isNull(maxCode)) {
            //查询数据库是否存在最大编码
            maxCode = customerInquiryMapper.selectMaxCode();
            if (StringUtils.isNull(maxCode)) {
                maxCode = "XJ" + ym + "0001";
                return maxCode;
            }
        }
        //判断现在最大编码是否为最新的编码
        if (!ym.equals(maxCode.substring(2, 6))) {
            maxCode = "XJ" + ym + "0001";
            return maxCode;
        }
        //如果到这里 是当月的最大编码  序号累加 XJ25020001
        String prefix = maxCode.substring(0, 6);
        String seq = maxCode.substring(6);
        int num = Integer.parseInt(seq);
        num++;
        maxCode = prefix + String.format("%04d", num);
        return maxCode;
    }
}
